// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// p [author] rhyme_lph
// p [email]  rhymelph@gmail.com
// T [github] https://github.com/rhymelph

import 'dart:io';

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/entities/setting_data_entity.dart';
import 'package:finance_app/src/widgets/bottom_sheet/category_bottom_sheet.dart';
import 'package:finance_app/src/widgets/bottom_sheet/default_bottom_sheet.dart';
import 'package:finance_app/src/widgets/bottom_sheet/stack_bottom_sheet.dart';
import 'package:finance_app/src/widgets/button/normal_popup_button.dart';
import 'package:finance_app/src/widgets/list_item/list_header_item.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/select/normal_radio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/camera/review_photo_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:flutter_sticky_header/flutter_sticky_header.dart';

@RouterMeta(paramName: "reviewPhotoPage")
class ReviewPhotoPage extends StatefulWidget {
  final String filepath;
  final bool isFromPhoto;
  final bool isCombine;
  const ReviewPhotoPage(
      {Key key, this.filepath, this.isFromPhoto, this.isCombine})
      : super(key: key);

  @override
  _ReviewPhotoPageState createState() => _ReviewPhotoPageState();
}

class _ReviewPhotoPageState extends State<ReviewPhotoPage>
    with PagePresenter<ReviewPhotoPage, ReviewPhotoProvider> {
  @override
  void initState() {
    provider.onInitData(widget.filepath, widget.isFromPhoto, widget.isCombine);
    super.initState();
  }

  @override
  Widget get buildWidget => StackBottomSheet(
        context: context,
        body: Scaffold(
          resizeToAvoidBottomInset: false,
          appBar: getDefaultAppBar(title: S.current.review_photo, actions: [
            NormalMorePopupButton(
              onSelect: provider.onSelectMenu,
              menuList: provider.menuList,
            ),
          ]),
          body: ColoredBox(
            color: aceBlackColor,
            child: Stack(
              children: [
                Center(
                  child: listBuilder<int>(
                    selector: (_, __) => __.filePathList.length,
                    builder: (BuildContext context, int value, Widget child) =>
                        ListView.builder(
                      shrinkWrap: true,
                      itemBuilder: _buildImageItem,
                      itemCount: value,
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
        isDismissible: true,
        bottomSheetBodyHasScrollView: true,
        bottomSheetHeight: MediaQuery.of(context).size.height * 3 / 4,
        bottomSheetBody: _buildBottomSheetBody,
        bottomSheetHeader: _buildBottomSheetHeader(),
        bottomSheetBodyScrollController: provider.scrollController,
        stackBottomSheetController: provider.stackBottomSheetController,
      );

  Widget _buildBottomSheetHeader() {
    return DefaultBottomSheet(
      title: S.current.select_category,
      hasClose: false,
      child: Text(''),
      bottomMargin: 20,
    );
  }

  Widget _buildBottomSheetBody(ScrollController controller) =>
      Transform.translate(
        offset: Offset(0, -1),
        child: ColoredBox(
          color: Colors.white,
          child: CategoryWidget(
            categories: Config.category,
            currentSelectCode: provider.currentSelectCategory,
            recentIds: provider.recentIds,
            selectCallBack: provider.onChangeSelectCode,
            scrollController: controller,
          ),
        ),
      );

  Widget buildSliver(String title, Map<String, dynamic> data) =>
      SliverStickyHeader(
        header: buildSliverHeader(title: title),
        sliver: SliverList(
          delegate: SliverChildBuilderDelegate(
            (context, i) => buildSliverItem(data, i),
            childCount: data.entries.length,
          ),
        ),
      );

  Widget buildSliverItem(Map<String, dynamic> data, int i) {
    return listBuilder<SettingDataCategory>(
        builder: (BuildContext context, SettingDataCategory value,
                Widget child) =>
            PressedOpacityButton(
              onPressed: () =>
                  provider.onChangeSelectCode(data.entries.toList()[i].value),
              padding: EdgeInsets.symmetric(
                horizontal: getDp(30),
                vertical: getDp(12),
              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  NormalRadio(
                    isSelect: value == data.entries.toList()[i].value,
                    title: data.entries.toList()[i].key,
                  ),
                ],
              ),
            ),
        selector: (_, __) => __.currentSelectCategory);
  }

  Widget buildSliverHeader({String title}) {
    final String _title = title.isEmpty ? "*" : title;
    return ListHeaderItem(
      title: _title,
    );
  }

  Widget _buildImageItem(BuildContext context, int index) {
    return listBuilder<String>(
        builder: (BuildContext context, String value, Widget child) =>
            Image.file(File(value)),
        selector: (_, __) => __.filePathList[index]);
  }
}
